package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.mould.MouldWorkRecordDTO;
import com.jcfk.eam.domain.query.mould.MouldWorkRecordQuery;
import com.jcfk.eam.service.mould.MouldWorkRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 通用模具操作记录
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-11-13
 */
@Api(tags = {"通用模具操作记录管理"})
@RestController
@RequestMapping("eammodworkrecord")
public class MouldWorkRecordController {
    @Autowired
    private MouldWorkRecordService mouldWorkRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamModWorkRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询通用模具操作记录")
    @PostMapping("/queryByPage")
    public RetPage<MouldWorkRecordDTO> queryByPage(@RequestBody ReqPageParam<MouldWorkRecordQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<MouldWorkRecordQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("workTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<MouldWorkRecordDTO> page = new DataPage<MouldWorkRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            MouldWorkRecordDTO.class);
        page = mouldWorkRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  通用模具操作记录
     * 根据id 查询详情
     */
    @ApiOperation("查询通用模具操作记录")
    @GetMapping("/get/{id}")
    public RetResult<MouldWorkRecordDTO> get(@PathVariable("id") String id) {
            MouldWorkRecordDTO item = mouldWorkRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理通用模具操作记录
     */
    @ApiOperation("保存通用模具操作记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "通用模具操作记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid MouldWorkRecordDTO eamModWorkRecord) {
        // 数据校验
        RetResult ret = mouldWorkRecordService.isValid(eamModWorkRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamModWorkRecord.getId())){
            // 新增
            mouldWorkRecordService.insert(eamModWorkRecord);
        } else{
            // 更新
            mouldWorkRecordService.update(eamModWorkRecord);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除通用模具操作记录
     */
    @ApiOperation("批量删除通用模具操作记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "通用模具操作记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody MouldWorkRecordDTO eamModWorkRecord) {
        return RetResult.ok(mouldWorkRecordService.logicDeleteList(eamModWorkRecord));
    }





    /**
     *  删除通用模具操作记录
     */
    @ApiOperation("删除通用模具操作记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "通用模具操作记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        mouldWorkRecordService.deleteItem(id);
        return RetResult.ok();
    }
}
